﻿using System.Collections;
using System.Collections.Generic;
using System.Net;
using com.tdb.foundation;
using com.tdb.posion.protocol.protobuf;
using Script;
using Script.Net;
using UnityEngine;
using UnityEngine.Events;

public class NetManager : MonoBehaviour
{

	public static NetManager Instance;
	
	#region Udp
	private UdpHelper _udpHelper;
	/// <summary>
	/// 消息处理器字典
	/// </summary>
	private readonly Dictionary<Cmds, UnityAction<Message, IPEndPoint>> _udpMessageHandlersDic = new Dictionary<Cmds, UnityAction<Message, IPEndPoint>>();
	public void SendUdpMessage(Message msg, IPEndPoint target)
	{
		_udpHelper.SendMessage(NetUtil.ConvertMessage2ByteArray(msg), target);
	}

	public void StartUdpListen()
	{
		if (_udpHelper == null)
		{
			_udpHelper = new UdpHelper(OnUdpDataArrive);
		}

		StartCoroutine(_udpHelper.ListenUdpMsg());
	}
	
	/// <summary>
	/// 处理接收到的msg
	/// </summary>
	private void HandlerMessage(Message msg, IPEndPoint remoteEP)
	{
		
	}
	/// <summary>
	/// 注册udp消息的处理器
	/// </summary>
	public void RegisterUdpMessageHandler(Cmds cmd, UnityAction<Message, IPEndPoint> callback)
	{
		_udpMessageHandlersDic.AddOrReplace(cmd, callback);
	}
	/// <summary>
	/// 解注udp消息的处理器
	/// </summary>
	public void UnRegisterUdpMessageHandler(Cmds cmd)
	{
		_udpMessageHandlersDic.Remove(cmd);
	}
	public void CloseUdp()
	{
		if (_udpHelper != null)
		{
			_udpHelper.Close();
		}
	}

	private void OnUdpDataArrive(byte[] data, IPEndPoint remoteEP)
	{
		var msg = NetUtil.ByteArray2Message(data);
		if (_udpMessageHandlersDic.ContainsKey(msg.Cmds))
		{
			if (_udpMessageHandlersDic[msg.Cmds] != null)
			{
				_udpMessageHandlersDic[msg.Cmds](msg, remoteEP);
			}
			else
			{
				Debug.LogError("Not Found Handler to Cmds:" + msg.Cmds);
			}
		}
		else
		{
			Debug.LogError("Not Found Handler to Cmds:" + msg.Cmds);
		}
	}
	#endregion

	#region TcpServer

	private TcpServer _tcpServer;
	public void StarTcpServer()
	{
		_tcpServer = new TcpServer(OnTcpServerData);
	}

	private void OnTcpServerData(byte[] data, TcpClientSocketData client)
	{
		Debug.Log("服务端收到"+System.Text.Encoding.Default.GetString(data));
		_tcpServer.SendTo(System.Text.Encoding.Default.GetBytes("Success"), client);
	}

	private void CloseServer()
	{
		if (_tcpServer != null)
		{
			_tcpServer.Close();
		}
	}
	
	#endregion
	#region TcpClient

	private TcpClient _tcpClient;

	public void StarTcpClient(IPEndPoint target)
	{
		_tcpClient = new TcpClient(OnTcpClientDataArrive);
		_tcpClient.ConnectServer(target, (ri) =>
		{
			
		});
	}

	public void ClientSendData(byte[] data)
	{
		_tcpClient.SendData(data, (ri) =>
		{
			Debug.Log("客户端发信成功");
		});
	}
	
	private void OnTcpClientDataArrive(byte[] data)
	{
		Debug.Log("客户端收到"+System.Text.Encoding.Default.GetString(data));
		
	}

	private void CloseClient()
	{
		if (_tcpClient != null)
		{
			_tcpClient.Close();
		}
	}
	#endregion


	void Update()
	{
		if (_tcpServer != null)
		{
			_tcpServer.MainCoroutine().MoveNext();
		}

	}

	void Awake()
	{
		Instance = this;
		DontDestroyOnLoad(this);
	}
	
	void OnDestroy()
	{
		CloseUdp();
		CloseClient();
		CloseServer();
		
	}
	
	
}
